﻿using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnitySheet;
using System.IO;

public class ReadWriteData : MonoBehaviour {
    public string mainNodeName = "course";
    public string fileName = "OneCourseData";
    string filePath;
    private void Awake()
    {
        filePath = Application.streamingAssetsPath + @"/" + fileName + ".xml";
        if (File.Exists(filePath))
            Options_XML.XML2Options(filePath,mainNodeName);
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("write");
            WriteSheet();
        }
        else if (Input.GetKeyDown(KeyCode.R))
        {
            Debug.Log("read");
            ReadXML();
        }
    }
    void WriteSheet()
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement mainElement = xmlDoc.CreateElement(mainNodeName);

        Options_XML.Options2XML(xmlDoc,mainElement);
        Sheet_XML.SheetToXML(SheetManager.sheetManager.selectedSheet, xmlDoc, mainElement);
        SheetContent_XML.SheetContent2XML(SheetManager.sheetManager.selectedSheet, xmlDoc, mainElement);
        xmlDoc.AppendChild(mainElement);
        xmlDoc.Save(filePath);
    }

    void ReadXML()
    {
        SheetContent_XML.XML2SheetContents(filePath, Sheet_XML.XMLToSheet(filePath, mainNodeName), mainNodeName);
    }
    
}
